package com.his.service.impl;

import com.his.common.Rsp;
import com.his.entity.Application;
import com.his.entity.Order;
import com.his.entity.Patient;
import com.his.entity.TotalOrder;
import com.his.mapper.ApplicationMapper;
import com.his.mapper.OrderMapper;
import com.his.mapper.PatientMapper;
import com.his.service.OrderService;
import com.his.utils.PayUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:13:58
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ApplicationMapper applicationMapper;
    @Resource
    private PatientMapper patientMapper;

    private int num=50;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer id) {
        return this.orderMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param order 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Order> queryAll(Order order) {
        return this.orderMapper.queryAll(order);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        this.orderMapper.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        this.orderMapper.update(order);
        return this.queryById(order.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.orderMapper.deleteById(id) > 0;
    }

    /**
     * 查询病人的订单,根据病人的id
     *
     * @param patientPhone 病人id
     * @return 返回订单数据
     */
    @Override
    public Rsp getByPatientPhone(String patientPhone) {
        Patient patient = patientMapper.getByTellphone(patientPhone);
        if (patient == null) {
            return Rsp.success(null);
        }
        Integer patientId = patient.getId();
        //查询手术费用
        List<Order> operation = orderMapper.queryAll(new Order(patientId, 1));
        //查询药品费用
        List<Order> medicinal = orderMapper.queryAll(new Order(patientId, 2));
        //查询护理费用
        List<Order> nursing = orderMapper.queryAll(new Order(patientId, 3));
        //查询住院记录
        Application application = applicationMapper.queryByPatientId(patientId.toString());
        TotalOrder totalOrder = new TotalOrder(operation, medicinal, nursing, application);
        return Rsp.success(totalOrder);
    }

    @Override
    public Rsp payAll(Integer[] id) {
        orderMapper.payAll(id);
        return Rsp.success(null);
    }

    @Override
    public Rsp payApplication(Integer id) {
        return applicationMapper.update(new Application(id, 1)) > 0 ? Rsp.success(null) : Rsp.fail().msg("住院费用支付失败");
    }

    /**
     * 通过ID逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public Rsp deleteByIdLogic(Integer id) {
        Integer deleteByIdLogic = orderMapper.deleteByIdLogic(id);
        if (deleteByIdLogic > 0) {
            return Rsp.success(null).msg("删除成功");
        } else {
            return Rsp.fail().msg("删除失败");
        }
    }

    @Override
    public Rsp getPayQRCode(Order pay) {
        PayUtil.Order order = PayUtil.createQrCode(pay.getTotalMoney());
        //调用线程查询
        final Timer timer = new Timer();
        final OrderServiceImpl o = new OrderServiceImpl();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                o.num = --o.num;
                //执行查询方法
                boolean flag = PayUtil.query(order.getNo());
                if (flag || o.num <= 0 ) {
                    if(flag) {
                        //执行修改支付状态
                        pay.setStatus("1");
                        orderMapper.update(pay);
                    }
                    timer.cancel();
                }
            }
        }, 5000, 1000);
        return Rsp.success(order);
    }

    @Override
    public Rsp payQuery(String orderNo) {
        return orderMapper.payQuery(orderNo)>0?Rsp.success(true):Rsp.success(false);
    }
}
